package itbour.onetouchshow.activity.userinfo;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;

import butterknife.BindView;
import butterknife.OnClick;
import itbour.onetouchshow.R;
import itbour.onetouchshow.activity.web.CommWebActivity;
import itbour.onetouchshow.activity.web.CreateOrderActivity;
import itbour.onetouchshow.adapter.PrivilegeAdapter;
import itbour.onetouchshow.adapter.VipPriceAdapter;
import itbour.onetouchshow.base.mvp.MVPBaseActivity;
import itbour.onetouchshow.bean.UserDetailInfoBean;
import itbour.onetouchshow.bean.VipInfoBean;
import itbour.onetouchshow.constants.AppConst;
import itbour.onetouchshow.widget.NoScrollGridView;

/**
 * Created by guoh on 2018/3/27.
 * 功能描述：尊享会员
 * 需要的参数：Vip推荐实体类
 */

public class VipDetailActivity extends MVPBaseActivity<VipDetailContract.View, VipDetailPresenter> implements VipDetailContract.View {

    private static final String INTRODUCE_INFO = "INTRODUCE_INFO";
    private static final String USER_TAG = "USER_TAG";
    /**
     * 不同页面跳转逻辑
     */
    public static String TAG_ME = "TAG_ME";
    public static String TAG_PLAY = "TAG_PLAY";
    @BindView(R.id.gv_privilege)
    NoScrollGridView gvPrivilege;
    @BindView(R.id.iv_vip_logo)
    ImageView ivVipLogo;
    @BindView(R.id.tv_update_vip)
    TextView tvUpdateVip;
    @BindView(R.id.tv_vip_tip)
    TextView tvVipTip;
    @BindView(R.id.tv_arrive_time)
    TextView tvArriveTime;
    @BindView(R.id.btn_open_now)
    Button btnOpenNow;
    @BindView(R.id.tv_vip_agreement)
    TextView tvVipAgreement;
    @BindView(R.id.iv_bottom_img)
    ImageView ivBottomImg;
    //    private UserDetailInfoBean.UserInfoBean userInfoBean;
    @BindView(R.id.gv_price)
    NoScrollGridView gvPrice;
    private PrivilegeAdapter adapter;
    private int clickPosition = 0;
    private VipInfoBean.IntroduceInfoBean introduceInfoBean;
    private VipPriceAdapter vipPriceAdapter;

    /**
     * @param context
     * @param introduceInfoBean
     * @param tag               标记作品过来还是个人中心过来
     * @return
     */
    public static Intent getLaunchIntent(Context context, VipInfoBean.IntroduceInfoBean introduceInfoBean, String tag) {
        Intent intent = new Intent(context, VipDetailActivity.class);
        intent.putExtra(INTRODUCE_INFO, introduceInfoBean);
        intent.putExtra(USER_TAG, tag);
        Bundle bundle = new Bundle();
        intent.putExtras(bundle);
        return intent;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.ac_vip_introduce;
    }

    @Override
    protected void onStart() {
        super.onStart();
        mPresenter.loadUserDetailInfo();
    }


    @Override
    protected void afterCreate(Bundle bundle) {
        introduceInfoBean = (VipInfoBean.IntroduceInfoBean) getIntent().getSerializableExtra(INTRODUCE_INFO);
        AppConst.BY_VIP_TAG = getIntent().getStringExtra(USER_TAG);

        vipPriceAdapter = new VipPriceAdapter(VipDetailActivity.this);
        vipPriceAdapter.replaceAll(introduceInfoBean.getVipGoods());
        if (introduceInfoBean.getVipGoods().size() > 0) {
            introduceInfoBean.getVipGoods().get(0).setSelected(true);
        }
        gvPrice.setAdapter(vipPriceAdapter);
        gvPrice.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                for (VipInfoBean.IntroduceInfoBean.VipGoodsBean goodsBean : vipPriceAdapter.getData()) {
                    goodsBean.setSelected(false);
                }
                vipPriceAdapter.getItem(position).setSelected(true);
                clickPosition = position;
                vipPriceAdapter.notifyDataSetChanged();
            }
        });
        if (!TextUtils.isEmpty(introduceInfoBean.getBottomImg().getUrl())) {
            // Glide.with(getViewContext()).load(ImageLoader.getShrinkImageUrl(introduceInfoBean.getBottomImg().getUrl(), introduceInfoBean.getBottomImg().getW(), introduceInfoBean.getBottomImg().getH())).into(ivBottomImg);
            Glide.with(getViewContext()).load(introduceInfoBean.getBottomImg().getUrl()).into(ivBottomImg);
        }
//        adapter = new PrivilegeAdapter(getViewContext());
//        adapter.replaceAll(DataCreater.getVipPrivilege());
//        gvPrivilege.setAdapter(adapter);
    }

    @Override
    protected void initTitle() {
        super.initTitle();
        setTitleText("尊享会员");
    }

    @Override
    public void loadSucceed(String result) {

    }

    @Override
    public void loadFaild(String error) {

    }


    private void setUiIsVip(UserDetailInfoBean.UserInfoBean userInfoBean) {
        tvUpdateVip.setVisibility(View.GONE);
        ivVipLogo.setVisibility(View.VISIBLE);
        tvVipTip.setVisibility(View.GONE);
        tvArriveTime.setVisibility(View.VISIBLE);
        tvArriveTime.setText("到期时间: " + userInfoBean.getVip().getExpiryTime());
        btnOpenNow.setText("立即续费");

    }

    private void setUiNotVip() {
        tvUpdateVip.setVisibility(View.VISIBLE);
        ivVipLogo.setVisibility(View.GONE);
        tvVipTip.setVisibility(View.VISIBLE);
        tvArriveTime.setVisibility(View.GONE);
        btnOpenNow.setText("立即开通");
    }


    @OnClick({R.id.btn_open_now, R.id.tv_vip_agreement})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_open_now:
                startActivity(CreateOrderActivity.getLaunchIntent(VipDetailActivity.this, vipPriceAdapter.getItem(clickPosition).getItemId() + "", vipPriceAdapter.getItem(clickPosition).getPriceId() + "", true));
                break;
            case R.id.tv_vip_agreement:
                Bundle bundle = new Bundle();
                bundle.putString(AppConst.WEBTYPE, AppConst.TYPE_VIP_AGREEMENT);
                openActivity(CommWebActivity.class, bundle);
                break;
        }
    }


    @Override
    public void loadUserDetailSuccess(String response) {
        UserDetailInfoBean userDetailInfoBean = new Gson().fromJson(response, UserDetailInfoBean.class);
        if (userDetailInfoBean == null) {
            return;
        }
        if (userDetailInfoBean != null && userDetailInfoBean.getUserInfo().getVip() != null && userDetailInfoBean.getUserInfo().getVip().getIsVip() == 1) {
            setUiIsVip(userDetailInfoBean.getUserInfo());
        } else {
            setUiNotVip();
        }
    }


}
